﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using Wicresoft.Busi.BizLogic;
using Wicresoft.Busi.Entity;

public partial class Car_VehicleInfoView : BaseAuthenticationPage
{
    public string VehicleID
    {
        get
        {
            if (ViewState["VehicleID"] != null)
                return ViewState["VehicleID"].ToString();
            else
                return string.Empty;
        }
        set
        {
            ViewState["VehicleID"] = value;
        }

    }

    protected void Page_Load(object sender, System.EventArgs e)
    {
        // Put user code to initialize the page here
        if (!Page.IsPostBack)
        {
            initVehicleType();
            initVehicle();
            if (Request.QueryString["ID"] != null && Request.QueryString["ID"] != "")
            {
                VehicleID = Request.QueryString["ID"].ToString();
                LoadVehicleInfo();
            }
        }
    }

    private void initVehicleType()
    {
        DataTable dt = BLDic_Vehicle.GetVehicleType();
        this.ddlVehicleType.DataSource = dt;
        this.ddlVehicleType.DataTextField = Dic_Vehicle.SQLCOL_PROPERTYNAME;
        this.ddlVehicleType.DataValueField = Dic_Vehicle.SQLCOL_PROPERTYID;
        this.ddlVehicleType.DataBind();
    }

    private void initVehicle()
    {
        DataTable dt = BLVehicles.GetVehicles(int.Parse(this.ddlVehicleType.SelectedValue));
        dt.Columns.Add("VehicleDesc", typeof(string));
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            DataRow dr = dt.Rows[i];
            dr["VehicleDesc"] = dr[vwVehicles.SQLCOL_VEHICLENO].ToString() + "（" + dr[Vehicles.SQLCOL_VEHICLENAME].ToString() + "）";
        }
        this.ddlVehicle.DataSource = dt;
        this.ddlVehicle.DataTextField = "VehicleDesc";
        this.ddlVehicle.DataValueField = vwVehicles.SQLCOL_ID;
        this.ddlVehicle.DataBind();
        ListItem item = new ListItem("-- 请选择 --", "0");
        this.ddlVehicle.Items.Insert(0, item);

    }

    private void LoadVehicleInfo()
    {
        if (this.ddlVehicle.SelectedIndex == 0)
            VehicleID = Request.QueryString["ID"];
      
        Vehicles info = new Vehicles(int.Parse(VehicleID));
        info.Load();

        this.ddlVehicleType.SelectedValue = info.VehicleType.ToString();
        this.lblVehicleType.Text = BLDic_Vehicle.GetPropertyName(VehiclePropertyType.VEHICLETYPE, info.VehicleType);
        this.lblVehicleNo.Text = info.VehicleNo + "（" + BLDic_Vehicle.GetPropertyName(VehiclePropertyType.VEHICLESTATUS, int.Parse(info.Flag)) + "）";
        this.lblVehicleName.Text = info.VehicleName;
        this.lblSeatsCount.Text = info.SeatsCount.ToString();
        this.lblDescription.Text = info.Description;
        if (info.Picture == null || info.Picture.Equals(string.Empty))
            this.imgVehicle.ImageUrl = "../images/NoPreview.jpg";
        else
        {
            this.imgVehicle.ImageUrl = "../images/Vehicles/" + info.Picture;
            this.hlImg.HRef = "../images/Vehicles/" + info.Picture;
        }
        tbDriverPhone.Text = info.DriverPhone;
    }

    protected void ddlVehicleType_SelectedIndexChanged(object sender, System.EventArgs e)
    {
        initVehicle();

    }

    protected void ddlVehicle_SelectedIndexChanged(object sender, System.EventArgs e)
    {
        VehicleID = this.ddlVehicle.SelectedValue;
        LoadVehicleInfo();
    }
}
